
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */


package oxygen.extensions.tomcat;

import java.security.Principal;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.apache.catalina.realm.GenericPrincipal;
import org.apache.catalina.realm.RealmBase;

import oxygen.manager.FSGroupManager;
import oxygen.manager.FSUserPasswordManager;

public class FSUserPasswordManagerBasedRealm extends RealmBase {
  private String passwordFile;
  private String groupFile;
  private String name = "FSUserPasswordManagerBasedRealm";
  private FSUserPasswordManager passwordmgr;
  private FSGroupManager groupmgr;
  private boolean fsinitDone = false;
  
  private void fsinit() throws Exception {
    if(fsinitDone) return;
    Properties p = new Properties();
    p.setProperty("oxygen.manager.password.file", passwordFile);
    p.setProperty("oxygen.manager.group.file", groupFile);
    p.setProperty("oxygen.manager.password.digest.encoding", digestEncoding);
    p.setProperty("oxygen.manager.password.digest.algorithm", digest);
    
    passwordmgr = new FSUserPasswordManager();
    passwordmgr.init(p);
    
    groupmgr = new FSGroupManager();
    groupmgr.init(p);
  }
  
  public void setPasswordFile(String s) throws Exception {
    //System.out.println("FSUserPasswordManagerBasedRealm: setPasswordFile: " + s);
    passwordFile = s;
  }
  
  public void setGroupFile(String s) throws Exception {
    //System.out.println("FSUserPasswordManagerBasedRealm: setGroupFile: " + s);
    groupFile = s;
  }

  public void setName(String s) {
    name = s;
  }
  
  public String getName() {
    return name;
  }
  
  protected String getPassword(String username) {
    try {
      fsinit();
      passwordmgr.sync();
      String s2 = new String(passwordmgr.getEncryptedPassword(username));
      //System.out.println("FSUserPasswordManagerBasedRealm: getPassword: " + username + " = " + s2 + "||");
      return s2;
    } catch(Exception exc) {
      throw new RuntimeException(exc);
    }
  }

  protected Principal getPrincipal(String username) {
    try {
      fsinit();
      List groups0 = Arrays.asList(groupmgr.getGroupsForUser(username));
      //System.out.println("FSUserPasswordManagerBasedRealm: groups for: " + username + " = " + groups0 + "||");
      return new GenericPrincipal(this, username, getPassword(username), groups0);
    } catch(Exception exc) {
      throw new RuntimeException(exc);
    } 
  }

  protected String digest(String credentials)  {
    try {
      fsinit();
      String s2 = passwordmgr.encrypt(credentials);
      //System.out.println("FSUserPasswordManagerBasedRealm: digest for: " + credentials + " = " + s2 + "||");
      return s2;
    } catch(Exception exc) {
      throw new RuntimeException(exc);
    }
  }

}

